import 'dart:io';

import 'package:connectivity/connectivity.dart';

class NetworkUtil {
  /// 判断网络是否可用
  static Future<bool> isNetworkAvailable() async {
    try {
      final result = await InternetAddress.lookup("baidu.com");
      if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
        return true;
      }
    } on SocketException catch (_) {
      print("Network is error.");
      return false;
    }
  }

  ///获取网络类型
  static Future<int> networkType() async {
    var connectivityResult = await (Connectivity().checkConnectivity());
    if (connectivityResult == ConnectivityResult.mobile) {
      return 1;
    } else if (connectivityResult == ConnectivityResult.wifi) {
      return 2;
    } else {
      return 0;
    }
  }
}
